//
//  YSProductDetailBannerView.m
//  Yueshi
//
//  Created by Northwan on 15/7/22.
//  Copyright (c) 2015年 悦食中国. All rights reserved.
//

#import "YSProductDetailBannerView.h"
#import "SGFocusImageFrame.h"
#import "SGFocusImageItem.h"

@interface YSProductDetailBannerView()<SGFocusImageFrameDelegate>
@property (nonatomic,strong)SGFocusImageFrame *bannerView;
@property (nonatomic,strong)NSMutableArray *itemArray;

@end

@implementation YSProductDetailBannerView

- (instancetype)init
{
    self = [super init];
    if (self) {
        [self setFrame:CGRectMake(0, 0, SCREENW, SCREENW/1.15)];
    }
    return self;
}

-(void)setBanner:(NSArray *)banner{
    [self inflateTopDataWithArray:banner];
}

-(void)inflateTopDataWithArray:(NSArray *)bannerArray{
    [self.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
    if(!bannerArray.count)return;
    NSMutableArray *itemArray = [NSMutableArray arrayWithCapacity:bannerArray.count+2];
    for (int i = 0; i < bannerArray.count; i++)
    {
        NSString *img =  bannerArray[i];
        SGFocusImageItem *item = [[SGFocusImageItem alloc] initWithTitle:@"" image:img tag:i];
        [itemArray addObject:item];
    }
    
    if(bannerArray.count>1){
        SGFocusImageItem *startItem = [[SGFocusImageItem alloc] initWithTitle:@"" image:[bannerArray lastObject] tag:bannerArray.count-1];
        [itemArray insertObject:startItem atIndex:0];
        
        SGFocusImageItem *endItem = [[SGFocusImageItem alloc] initWithTitle:@"" image:[bannerArray firstObject] tag:bannerArray.count-1];
        [itemArray addObject:endItem];
    }
    
    SGFocusImageFrame *bannerView = [[SGFocusImageFrame alloc] initWithFrame:CGRectMake(0, 0, SCREENW,SCREENW/1.15) delegate:self imageItems:itemArray isAuto:YES];
    [bannerView scrollToIndex:0];
    [self addSubview:bannerView];
}

-(void)foucusImageFrame:(SGFocusImageFrame *)imageFrame didSelectItem:(SGFocusImageItem *)item{
    NSLog(@"-------------foucusImageFrame");
    
    if([_delegate respondsToSelector:@selector(bannerItemClick:)]){
        [_delegate bannerItemClick:item.tag];
    }
}

@end
